package at.mri.dogwatch.client.pages.videodetails;

import at.mri.dogwatch.client.pages.layout.MainPagePresenter;
import at.mri.dogwatch.client.place.NameTokens;
import at.mri.dogwatch.shared.proxy.VideoProxy;
import at.mri.dogwatch.shared.services.VideoRequestFactory.VideoRequestContext;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;

public class VideDetailsPresenter extends
		Presenter<VideDetailsPresenter.MyView, VideDetailsPresenter.MyProxy> {

	public interface MyView extends View {
		void initializeVideo(final String videoID);
	}
	
	private VideoProxy video;
	
	@Inject VideoRequestContext videoRequest;

	@ProxyCodeSplit
	@NameToken(NameTokens.videodetails)
	public interface MyProxy extends ProxyPlace<VideDetailsPresenter> {
	}

	@Inject
	public VideDetailsPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent
				.fire(this, MainPagePresenter.SLOT_SetCenterContent, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
	}
	
	@Override
	public void prepareFromRequest(PlaceRequest request) {
		Long id = Long.parseLong(request.getParameter(NameTokens.VIDEO_ID_PARAM, "-1"));
		
		//fetch the video-proxy for the given video-idobjec
		videoRequest.get(id).fire(new Receiver<VideoProxy>() {

			@Override
			public void onSuccess(VideoProxy response) {
				video = response;
				getView().initializeVideo(video.getYoutubeTag());
				
			}
			
			@Override
			public void onFailure(ServerFailure error) {
				//TODO
				super.onFailure(error);
			}
		});
	}
}
